package config

import "github.com/spf13/viper"

type server struct {
	Port int `validate:"required"`
}
var (
	ServerConfig server
)

func initSetting() {
	setConfigDefaultValue()
	viper.SetConfigName("application")
	err := viper.ReadInConfig()
	if err != nil {
		panic(err)
	}
	c := viper.AllSettings()
	read(c["server"], &ServerConfig)

	validateSetting()
}

func setConfigDefaultValue() {
	ServerConfig.setDefault()
}

func validateSetting() {
	ServerConfig.validate()
}

func (s *server) validate() {
	err := validate.Struct(s)
	if err != nil {
		panic(err)
	}
}

func (s *server) setDefault() {
	viper.SetDefault("server.port", "8080")
}
